<?php

namespace App\Http\Controllers\RBAC;

use App\RBAC\ResType;
use App\RBAC\Resource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ResourceController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function menu(Request $request)
    {
        return view('rbac/menu');
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $request->session()->put('status', 'current_user:'.Auth::user());
        return view('rbac/resource');
    }

    public function apiMenuIndex(Request $request)
    {
        $data['resTypes']=ResType::get();
        if($request->res_type_id){
            $res_type_id=$request->res_type_id;            
        }else{
            $res_type_id=$data['resTypes'][0]->id;
        }  
        $list=Resource::where('res_type_id',$res_type_id)->where('parent_id',0)->orderBy('parent_id','ASC')->orderBy('sort_id','ASC')->get();      
        $resources=[];
        foreach ($list as $item) {
            $childrens=Resource::where('parent_id',$item->id)->orderBy('sort_id','ASC')->get();
            foreach ($childrens as $value) {
                $value->childrens=[];
            }
            $item->childrens=$childrens;
        }

        $data['resources']=$list;
        $data['res_type_id']=$res_type_id;
        return $data;
    }

    public function apiIndex(Request $request)
    {

        if($request->res_type_id){
            $data['data']=Resource::where('res_type_id',$request->res_type_id)->orderBy('id',"ASC")->paginate(intval($request->per_page));
        }else{
            $data['data']=Resource::where('pid',0)->orderBy('id',"ASC")->paginate(intval($request->per_page));
        }

        $api_type=ResType::select('id')->where('name','api')->first();
        $data['childrens']=Resource::where('res_type_id',$api_type['id'])->orderBy('id',"ASC")->get();
        
        $uris=Resource::select('res_string')->get()->pluck('res_string');
        $data['ResTypes']=ResType::get();
        $app=app();
        $routes = $app->routes->getRoutes();
        $path=[];
        $index=0;
        foreach ($routes as $k=>$value){
           $uri=$value->uri();
            if(in_array('auth',$value->controllerMiddleware())&& !$uris->contains($uri)){
                $path[$index]['uri'] = $value->uri();
                $path[$index]['name'] = $value->getName();
                $path[$index]['methods'] = $value->methods();
                $index++;
            }
        }
        $data['routes']=$path;
        $data['api_type']=$api_type;
        return $data;
    }

    public function apiDelete(Request $request,Resource $resource)
    {
        $operations=$resource->operations;
        foreach ($operations as $key => $value) {
            $value->pivot->delete();
        }
        if($resource->delete()){
            return ['msg'=>'ok'];
        }else{
            return ['msg'=>'fail'];
        }
        
    }

    public function apiStore(Request $request)
    {
        if($request->id){
            $group = Resource::findOrFail(intval($request->id));
            $group->name=$request->name;
            $group->description=$request->description;
            $group->res_type_id=$request->res_type_id;
            $group->res_string=$request->res_string;
            if($group->save()){
                $group = Resource::findOrFail(intval($request->id));
            }
        }else{
            $group = Resource::firstOrCreate(['name' => $request->name,'res_string'=>$request->res_string,'res_type_id'=>$request->res_type_id,'description'=>$request->description]);
        }
        $oldpids=[];
        if($request->oldpids){
            $oldpids=$request->oldpids; 
            $childrens_res=Resource::whereIn('id',$oldpids)->get();
            foreach ($childrens_res as $value) {
                $value->pid=0;
                $value->save();
            }           
        }
        $pids=[];
        if($request->pids){
            $pids=$request->pids;
            $childrens_res=Resource::whereIn('id',$pids)->get();
            foreach ($childrens_res as $value) {
                $value->pid=$group->id;
                $value->save();
            }
        }

        return $group;
    }

    public function apiStoreMenu(Request $request)
    {
        $data=$request->data;
        $sort_id=0;
        $data_arr=[];
        foreach ($data as $item) {
            $sort_id++;
            $item['sort_id']=$sort_id;
            $childrens=$item['childrens'];
            $data_arr[$sort_id]=['id'=>$item['id'],'name'=>$item['name'],'sort_id'=>$item['sort_id']];
            $model=Resource::find($item['id']);
            $model->sort_id=$item['sort_id'];
            $model->save();
            if(!empty($childrens)){
                foreach ($childrens as $value) {
                    $sort_id++;
                    $value['sort_id']=$sort_id;
                    $value['parent_id']=$item['id'];
                    $data_arr[$sort_id]=['id'=>$value['id'],'name'=>$value['name'],'sort_id'=>$value['sort_id'],'parent_id'=>$item['id']];
                    $model=Resource::find($value['id']);
                    $model->sort_id=$value['sort_id'];
                    $model->parent_id=$item['id'];
                    $model->save();
                }
            }
        }
        return $data_arr;
    }



}
